/* 文件上传 */
var content = $("#currContent").val();
var warePicPath = $("#picPath").val();

function createUploader() {
	var uploader = new qq.FileUploader({
		action : content+'/common/uploadImg.shtm',
		element : document.getElementById('taskImg_uploader'),
		allowedExtensions : [ 'jpg', 'gif', 'png' ],
		acceptFiles : [ 'image/*' ],
		uploadButtonText : "",
		uploadButtonID : "taskImgUploadBtn",
		buttonClass : "grxxform-zgzs-add fl",
		debug : false,
		sizeLimit : 10 * 1024 * 1024,
		fileNumLimit :6,
		ifOverrange:ifOverranges,
		params : {
			fileType : "img",
			proVal : "TEACHER_PATH"
		},
		stopOnFirstInvalidFile : false,
		addtolistcallback : callback_addtolist,
		onProgress : callback_progress,
		onComplete : callback_complete
	});
}


function ifOverranges (limit){
	if ($('div[id^=taskImgDiv_]').length >= limit) {
			var tip = "只允许上传"+limit+"张以内的图片";
			if(limit==1){
				tip = "只允许上传1张图片";
			}
		alert(tip);
		return true;
	}
	return false;
}
function callback_addtolist(id, fileName) {
 	$("#taskImg_area").append(showAddToList(id, fileName, "taskImg"));
}
function callback_progress(id, fileName, loaded, total) {
 	showprogress(id, fileName, loaded, total, "taskImg");
}
function callback_complete(id, fileName, result) {
 	showcomplete(id, fileName, result, "taskImg");
	//console.log("complete:"+id);
}
function showprogress(id, fileName, loaded, total, elePre) {
	var percent = Math.round(loaded / total * 100)
	var width = 2 * percent;
	var proBarDivID = elePre+"Div_" + id ;
	$("#" + proBarDivID).html(
		"<img src='"+content+"/images/upload/waiting.gif'>"+
		"<span class=\"ml20\">"+ percent + "%<\/span>");
}
function showAddToList(id, fileName, elePre) {
	var str = "";
	str = " <div class=\"grxxform-zgzs-pic fl pr\" id=\"" + elePre+"Div_" + id +"\">";
	str += "</div>";
	return str;

}
function showcomplete(id, fileName, result, elePre) {
	var reg = new RegExp("\.jpg|\.gif|\.png", "g");
	var desc = fileName.toLowerCase().replace(reg, "");
	var imgAreaDivId = elePre+"Div_" + id;
	var subid = $("#" + imgAreaDivId);
	if (result.success == 'true') {
		var str = "";
		if(elePre=="taskImg"){
			str += "<img style=\"width:217px;height:132px;\" src=\""+warePicPath+result.imagePath+"\" \/>";
		}
		subid.html(str);
		subid.append('<input type="hidden" name="'+elePre+'_imageSaveName" value="'+ result.saveName +'" />');
		var myDiv = document.getElementById("taskImg_area");
		var div_s = myDiv.getElementsByTagName("div");
		if(div_s.length>=1){
			 $("#qualicationImg").removeClass("grxxform-zgzs-tip fl");
			 $("#qualicationImg").addClass("grxxform-zgzs-aip fl");
		}else{
			 $("#qualicationImg").removeClass("grxxform-zgzs-aip fl");
			 $("#qualicationImg").addClass("grxxform-zgzs-tip fl");
		}
		$('#'+imgAreaDivId).attr('onmouseover','javascript:onMouseOver('+'\''+imgAreaDivId+'\''+');');
		$('#'+imgAreaDivId).attr('onmouseleave','javascript:onMouseOut('+'\''+imgAreaDivId+'\''+');');
	} else {
		if(result.error&&window.console){
			$("#qualicationImg").removeClass("grxxform-zgzs-aip fl");
			$("#qualicationImg").addClass("grxxform-zgzs-tip fl");
			alert("上传失败！错误："+result.error);
		}
		$("#" + imgAreaDivId ).remove();
	}
}
function onMouseOver(id){
	if($("#"+id).find("em").length==0){
		$("#"+id).append('<em class="abs grxxform-zgzs-close"></em>');
	}
}
function onMouseOut(id){
	$("#"+id).find("em").remove();
}
/* 文件上传结束 */